﻿using Microsoft.VisualBasic;
using System;

namespace APP.HELPER.Utils
{
    //replace the number function from C++ and get from JAVA to C#
    /// <summary>
    /// Replace function "NumberHour", "MinuteToHour", "NumberMinute", "QuarterFromMonth"
    /// </summary>
    /// 
    public static class MyDateTime
    {
        private static int _d = 1;
        private static int _m = 1;
        private static int _y = DateTime.Now.Year;
        public static int Day
        {
            get
            {
                return MyDateTime._d;
            }
            set
            {
                MyDateTime._d = value;
            }
        }
        public static int Month
        {
            get
            {
                return MyDateTime._m;
            }
            set
            {
                MyDateTime._m = value;
            }
        }
        public static int Year
        {
            get
            {
                return MyDateTime._y;
            }
            set
            {
                MyDateTime._y = value;
            }
        }

        public unsafe static double MinuteToHour(double Minute)
		{
            return TimeSpan.FromMinutes(Minute).TotalHours;
		}
        public unsafe static double NumberHour(DateTime dt1, DateTime dt2)
		{
			//void* ptr = stackalloc void[20 / sizeof(void)];
            //*(double*)ptr = 0.0;
            //*(int*)(ptr + 16 / sizeof(void)) = MyDateTime.NumberMinute(dt1, dt2);
            //*(double*)ptr = MyDateTime.MinuteToHour(*(int*)(ptr + 16 / sizeof(void)));
            //*(double*)(ptr + 8 / sizeof(void)) = *(double*)ptr;
            //return *(double*)(ptr + 8 / sizeof(void));
            double hour = 0.0;
            double minute = MyDateTime.NumberMinute(dt1, dt2);
            hour = MyDateTime.MinuteToHour(minute);
            return hour;
		}
        public unsafe static int NumberMinute(DateTime dt1, DateTime dt2)
		{
            //void* ptr = stackalloc void[16 / sizeof(void)];
            //*(int*)(ptr + 8 / sizeof(void)) = 0;
            //*(long*)ptr = DateAndTime.DateDiff(DateInterval.Minute, dt1, dt2, FirstDayOfWeek.System, FirstWeekOfYear.System);
            //*(int*)(ptr + 8 / sizeof(void)) = int.Parse(((long*)ptr)->ToString());
            //*(int*)(ptr + 12 / sizeof(void)) = Math.Abs(*(int*)(ptr + 8 / sizeof(void)));
            //return *(int*)(ptr + 12 / sizeof(void));
            int i = 0;
            long j = DateAndTime.DateDiff(DateInterval.Minute, dt1, dt2, FirstDayOfWeek.System, FirstWeekOfYear.System);
            i = int.Parse(j.ToString());
            int result = Math.Abs(i);
            return result;
		}
        public unsafe static int QuarterFromMonth(int Month)
		{
			int count = 1;
			if (Month <= 3)
			{
				count = 1;
			}
			else
			{
				if (Month > 3 && Month <= 6)
				{
					count = 2;
				}
				else
				{
					if (Month > 6 && Month <= 9)
					{
						count = 3;
					}
					else
					{
						if (Month > 9 && Month <= 12)
						{
							count = 4;
						}
					}
				}
			}
            return count;
		}
        public unsafe static int MonthFromQuarter(int Quarter, int Month)
		{
			int m = Month;
			if (Quarter == 1)
			{
				if (m > 3)
				{
					m = 1;
				}
			}
			else
			{
				if (Quarter == 2)
				{
					if (m < 4 || m > 6)
					{
						m = 4;
					}
				}
				else
				{
					if (Quarter == 3)
					{
						if (m < 7 || m > 9)
						{
							m = 7;
						}
					}
					else
					{
						if (Quarter == 4 && m < 10)
						{
							m = 10;
						}
					}
				}
			}
			return m;
		}

        public static DateTime AddMinute(DateTime Date, int Minute)
        {
            DateTime result;
            try
            {
                result = DateAndTime.DateAdd(DateInterval.Minute, (double)Minute, Date);
            }
            catch
            {
                result = Date;
            }
            return result;
        }
        public static DateTime MinusMinute(DateTime Date, int Minute)
        {
            return DateAndTime.DateAdd(DateInterval.Minute, (double)(-(double)Minute), Date);
        }
        public static void GetValueFromString(string dt)
        {
            try
            {
                char[] separator = new char[]
				{
					'/',
					'-',
					'.'
				};
                string[] array = dt.Split(separator);
                if (array.Length == 1)
                {
                    MyDateTime._d = 0;
                    MyDateTime._m = 0;
                    MyDateTime._y = Convert.ToInt32(array[0].ToString());
                }
                else
                {
                    if (array.Length == 2)
                    {
                        MyDateTime._d = 0;
                        MyDateTime._m = Convert.ToInt32(array[0].ToString());
                        if (MyDateTime._m > 12)
                        {
                            MyDateTime._m = 0;
                        }
                        MyDateTime._y = Convert.ToInt32(array[1].ToString());
                    }
                    else
                    {
                        if (array.Length == 3)
                        {
                            MyDateTime._d = Convert.ToInt32(array[0].ToString());
                            MyDateTime._m = Convert.ToInt32(array[1].ToString());
                            if (MyDateTime._m > 12)
                            {
                                MyDateTime._d = 0;
                                MyDateTime._m = 0;
                            }
                            MyDateTime._y = Convert.ToInt32(array[2].ToString());
                            if (MyDateTime._d > DateTime.DaysInMonth(MyDateTime._y, MyDateTime._m))
                            {
                                MyDateTime._d = DateTime.DaysInMonth(MyDateTime._y, MyDateTime._m);
                            }
                        }
                    }
                }
                if (MyDateTime._y < 1901)
                {
                    MyDateTime._y = 1901;
                }
                else
                {
                    if (MyDateTime._y > 2199)
                    {
                        MyDateTime._y = 2199;
                    }
                }
            }
            catch
            {
            }
        }
    }
}
